package com.example.customizedgroupmessage;

import java.util.ArrayList;
import android.app.*;
import android.content.*;
import android.database.*;
import android.net.Uri;
import android.os.*;
import android.provider.*;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.*;

public class SMSGroupListView extends Activity {
	// 요청 코드
	public static final int REQUEST_CODE_SMSMEMBERLISTVIEW=1002;
	// Log TAG
	//private static final String TAG1 = "Group TAG";
	// 그룹 저장할 배열
	public ArrayList<Group> arrlistGroup = new ArrayList<Group>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_group_listview);

		arrlistGroup.clear();
		Group temp1 = new Group("-1", "전체 목록", getAllCount());
		arrlistGroup.add(temp1);
		
		Uri uri = ContactsContract.Groups.CONTENT_URI;

		String[] projection = new String[] {
		    ContactsContract.Groups._ID,
		    ContactsContract.Groups.TITLE,
		    ContactsContract.Groups.ACCOUNT_NAME,
		    ContactsContract.Groups.ACCOUNT_TYPE,
		    ContactsContract.Groups.DELETED,
		    ContactsContract.Groups.GROUP_VISIBLE
		};
		
		//String selection = ContactsContract.Groups.DELETED + "=0" + " AND " + 
		//						ContactsContract.Groups.GROUP_VISIBLE + "=1";
		String selection = null;
		String sortOrder = null;
		
		Cursor cursor = getContentResolver().query(uri, projection, selection, null, sortOrder);
		String groupId = null;
		String groupType = null;
		String groupTitle = null;
		String accountName = null;
		int groupCount = 0;

		while (cursor.moveToNext()) {
			/*
			Log.d(TAG1, "Id ===> " + cursor.getString(0));
			Log.d(TAG1, "Title ===> " + cursor.getString(1));
			Log.d(TAG1, "Account Name ===> " + cursor.getString(2));
			Log.d(TAG1, "Account Type ===> " + cursor.getString(3));
			Log.d(TAG1, "Deleted ===> " + cursor.getString(4));
			Log.d(TAG1, "Visible ===> " + cursor.getString(5));
			*/
			   
			groupId = cursor.getString(0);
			groupTitle = cursor.getString(1);
			groupType = cursor.getString(3);
			groupCount = getGroupSummaryCount(cursor.getString(0));
			
			Group temp = new Group(groupId, groupTitle, groupCount);
			arrlistGroup.add(temp);
		}
		cursor.close();
		
		ArrayList<String> arrGroup = new ArrayList<String>();
		for(int i=0; i<arrlistGroup.size(); i++){
			arrGroup.add(arrlistGroup.get(i).getGroupTitle() + " (" + arrlistGroup.get(i).getGroupCount() + ")");
		}
		
		// 어댑터 준비
		ArrayAdapter<String> Adapter;
		//Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrlistGroup);
		Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrGroup);

		// 리스트뷰에 어탭터 연결
		ListView list = (ListView)findViewById(R.id.sms_group_list);
		list.setAdapter(Adapter);
		// 이벤트 핸들러 연결
		list.setOnItemClickListener(mItemClickListener);
	}
	
	// OnClick 이벤트 핸들러
	AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Intent intent = new Intent(SMSGroupListView.this, SMSMemberListView.class);
			intent.putExtra("SelectedGroup", arrlistGroup.get(position).getGroupTitle());
			intent.putExtra("SelectedGroupId", arrlistGroup.get(position).getGroupId());
			startActivityForResult(intent, REQUEST_CODE_SMSMEMBERLISTVIEW);
		}
	};
	
	
	// 그룹 멤버 수 카운트하기
	private int getGroupSummaryCount(String groupId) {

		Uri uri = ContactsContract.Groups.CONTENT_SUMMARY_URI;
		String[] projection = new String[]  { ContactsContract.Groups.SUMMARY_COUNT };
		String selection = ContactsContract.Groups._ID + "=" + groupId;

		Cursor cursor = getContentResolver().query(uri, projection, selection, null, null);
		int count = 0;
		
		while (cursor.moveToNext()) {
			count = cursor.getInt(0);
		}
		cursor.close();
		return count;
	}
	
	// 전체 멤버 수 카운트하기
	private int getAllCount() {

		Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, Contacts.HAS_PHONE_NUMBER + " = 1", null, null);
		int count = 0;
		
		while (cursor.moveToNext()) {
			count++;
		}
		cursor.close();
		return count;
	}


}